package cn.com.czpb.client.module.personal.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.usercenter.MyInviteBean;
import cn.com.czpb.client.model.usercenter.MyInviteEntity;
import cn.com.czpb.client.module.personal.TopInvite;
import cn.com.czpb.client.module.personal.adapter.MyInviteAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * Created by Administrator on 2017/12/1.
 */

public class InviteFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {

    @BindView(R.id.myinvite_srl)
    SmartRefreshLayout swipe_layout;
    @BindView(R.id.myinvite_rv)
    RecyclerView myinvitelistview;
    MyInviteAdapter adapter;
    List<MyInviteBean> list;
    TopInvite topInvite;
    private boolean mFlagRefresh = true;//刷新还是加载
    private int Page = 1;
    private static final int PageSize = 3;
    public static InviteFragment newInstance(){
        InviteFragment fragment = new InviteFragment();
        return fragment;
    }
    @Override
    public int setMyContentView() {
        return R.layout.fragment_invitepage;
    }

    @Override
    public void initView() {
        initadapter();
        if (spu.getUser()!=null){
//            myinvitelistview.setVisibility(View.VISIBLE);
            topInvite.showLoginStatus(spu.getUser().getInvitcode());
        }else{
            topInvite.showNoLoginStatus();
//            myinvitelistview.setVisibility(View.GONE);
            Toast.makeText(activity,"未登录",Toast.LENGTH_SHORT).show();
        }
        adapter.addHeaderView(topInvite.getView());
    }

    private void initadapter() {
        topInvite = TopInvite.instance(activity);
        list = new ArrayList<>();
        adapter = new MyInviteAdapter(list,activity);
        adapter.setOnLoadMoreListener(this,myinvitelistview);
        myinvitelistview.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        myinvitelistview.setAdapter(adapter);
        swipe_layout.setRefreshHeader(new ClassHeader_other(activity));
        swipe_layout.setRefreshFooter(null);
        swipe_layout.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
        if(spu.getUser() != null){
            getInviteList();
        }else{
            adapter.loadMoreEnd();
        }
    }

    private void getInviteList() {
        if(spu.getUser() != null) {
            Map<String, String> map = new HashMap<>();
            map.put("uid", spu.getUser().getUid());
            Factory.provideHttpService().myinvite(ParamUtils.getRequestParam(map))
                    .onBackpressureBuffer()
                    .subscribeOn(Schedulers.io())
                    .filter(new Func1<MyInviteEntity, Boolean>() {
                        @Override
                        public Boolean call(MyInviteEntity myInviteEntity) {
                            return true;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<MyInviteEntity>() {
                        @Override
                        public void call(MyInviteEntity myInviteEntity) {
                            LogUtils.e("code--" + myInviteEntity.code);
                            if (mFlagRefresh) {
                                swipe_layout.finishRefresh();
                            }
                            if ("200".equals(myInviteEntity.code)) {
                                topInvite.setinvitnum("我共邀请了"+myInviteEntity.nums+"位好友");
                                if (mFlagRefresh) {
                                    adapter.setNewData(myInviteEntity.inviteinfo);
                                    adapter.notifyDataSetChanged();
                                } else {
                                    adapter.addData(myInviteEntity.inviteinfo);
                                    adapter.notifyDataSetChanged();
                                    adapter.loadMoreComplete();
                                }
                                LogUtils.i("size-->" + adapter.getData().size());
                                if (myInviteEntity.inviteinfo.size() < PageSize) {
                                    //关闭刷新
                                    adapter.loadMoreEnd(true);
                                }
                                LogUtils.e(list.size() + "");
                            }else{
                                if("209".equals(myInviteEntity.code)){
                                    topInvite.setinvitnum("我共邀请了0位好友");
                                }
                            }
                        }

                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if (mFlagRefresh) {
                                swipe_layout.finishRefresh();
                            }
                            adapter.loadMoreFail();
                            LogUtils.i("throwable--" + throwable.getMessage());
                        }
                    });
            //得到list，初始化adapter
        }
    }



    @Override
    public void onLoadMoreRequested() {
        mFlagRefresh = false;
        Page++;
        getInviteList();
    }

    @Override
    public void onRefresh(final RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(spu.getUser() != null){
                    Page = 1;
                    mFlagRefresh = true;
                    getInviteList();
                }else{
                    refreshlayout.finishRefresh();
                }
            }
        }, 2000);
    }
}
